<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="servlets.*"%>
<%@ page import="org.hibernate.SessionFactory"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="java.util.List"%>
<%@ page import="hibernate.model.*"%>
<%@ page import="hibernate.util.HibernateUtil"%>
<%@ page import="hibernate.method.*"%>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<script>
$("a.games").click(function() {
		$("#games_content").hide().fadeIn(1000).load("games/gameProfile.jsp?game_id="+ $(this).attr('id'));
		return false;
	});
</script>

<div class="page-header">
	<h2 class="page-title">Nasze gry</h2>
</div>
<div id="content">
<br />
<%
						SessionFactory sessionFactory = HibernateUtil
								.getSessionAnnotationFactory();
						Session s = sessionFactory.openSession();
						s.beginTransaction();
						GameMethods gm = new GameMethods();
							response.setContentType("text/html");
							List<Game> games = gm.listAllGames(sessionFactory, s);
							FeedbackMethods fb = new FeedbackMethods();
					%> 
			<table>
<%
		if (games.size() > 0) {
	for (Game g1  : games) {
		int game_id = g1.getGame_id();
		String gameName = g1.getName();		%>
<tr>
<td>
		<div class="col-md-4 col-sm-6" style="width: 135%;">
											<div class="team-member">
												 <div class="member-thumb">
												<!--	<img src="images/team3.jpg" alt=""> -->
												</div>
												<div class="member-infos">
													<h4 class="member-name">
													<%out.println("<a class=\"games\" id=\""+ game_id + "\" href=\"#\">" + gameName + "</a>"); %></h4>
													<span class="member-role">
													<%RateMethods rm = new RateMethods();
													out.println("Ogólna ocena uzytkownikow: "+ rm.returnAverageRateByGame(sessionFactory, s, game_id));
													if(session.getAttribute("user_id") != null){
														if(rm.findGameRateByUserGame(sessionFactory, s, (Integer) session.getAttribute("user_id"), game_id).size()==0)
															out.println("Twoja ocena: Brak");
														else 
															out.println("Twoja ocena: "+ rm.findGameRateByUserGame(sessionFactory, s, (Integer) session.getAttribute("user_id"), game_id).get(0).getRate());
														 
													}
													%>
													<br />
													<%
													int commentsCount = fb.countFbByGameId(sessionFactory, s, game_id);
													out.println("Komentarzy: "+ commentsCount); %>
													</span>
													<%
													GameCategoryMethods gcat = new GameCategoryMethods();
													List<String> gamecategories = new ArrayList<String>();
													gamecategories = gcat.listCategoriesByGame(sessionFactory, s, game_id);
													if (gamecategories == null) out.println("Gra nie ma jeszcze zadnej kategorii");
													else if (gamecategories.size() >0) {out.println("Kategorie: ");
													for (String gc : gamecategories){
														out.println(gc+", ");
													}
													}
													%>
												</div>
											</div>
										</div>
										<!-- /.col-md-4 -->
									
									</td>
									</tr>
									<tr> <td><br /></td></tr>
								
		<%
		}
	%>
	
	</table>
	<%
		}
		s.getTransaction().commit();
		%>

</div>		
		